using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.DataStructure;
using Entity;

namespace AutoGear.BLL
{
    public class StockAlert
    {
        private int _productId;
        public int ProductId
        {
            get { return this._productId; }
            set { this._productId = value; }
        }

        private string _productName;
        public string ProductName
        {
            get { return this._productName; }
            set { this._productName = value; }
        }

        private string _productImage;
        public string ProductImage
        {
            get { return this._productImage; }
            set { this._productImage = value; }
        }

        private int _stockAmount;
        public int StockAmount
        {
            get { return this._stockAmount; }
            set { this._stockAmount = value; }
        }

        private string _productSize;
        public string ProductSize
        {
            get { return this._productSize; }
            set { this._productSize = value; }
        }

        private string _productItemNumber;
        public string ProductItemNumber
        {
            get { return this._productItemNumber; }
            set { this._productItemNumber = value; }
        }

        public StockAlert(TblProductStock stock)
        {
            Product product = Product.Load(stock.ProductId);
            this.ProductId = product.ProductId;
            this.ProductImage = product.ProductImageSmallThumbnail;
            this.ProductItemNumber = product.ItemNumber;
            this.ProductName = product.ProductName;
            this.ProductSize = stock.Size;
            this.StockAmount = stock.StockAmount;
        }

        public static StockAlert Load(int productId)
        {
            ECommerce.DAL.CustomCollection< TblProductStock> stock = TblProductStockExtended.GetProductStockByProductId(productId);
            if (stock.Count > 0)
            {            
                StockAlert alert = new StockAlert(stock[0]);
                return alert;
            }
            return null;
            
        }

        public static bool IsStockAlert(int productId)
        {
            ReadWriteCustomCollection<ProductStock> stocks = ProductStock.GetStockByProductId(productId);
            if (stocks.Count > 0)
            {
                if( stocks[0].StockAmount<= SiteSettings.StockAlertLevel){
                    return true;
                }
            }
            return false;
        }

        public static ReadWriteCustomCollection<StockAlert> GetStockOutList()
        {
            ReadWriteCustomCollection<StockAlert> alerts = new ReadWriteCustomCollection<StockAlert>();
            ECommerce.DAL.CustomCollection<TblProductStock> stockAlerts = TblProductStockExtended.GetStockOutItems(SiteSettings.StockAlertLevel);
            for (int i = 0; i < stockAlerts.Count; i++)
            {
                try
                {
                    TblProductStock stock = stockAlerts[i];
                    Product product = Product.Load(stock.ProductId);
                    StockAlert alert = new StockAlert(stock);
                    alerts.Add(alert);
                }
                catch (Exception ex) { }
            }

            return alerts;
        }

        public static ReadWriteCustomCollection<StockAlert> GetStockOutListPaged(string orderBy, int startIndex, int pageSize)
        {
            ReadWriteCustomCollection<StockAlert> alerts = new ReadWriteCustomCollection<StockAlert>();
            ECommerce.DAL.CustomCollection<TblProductStock> stockAlerts = TblProductStockExtended.GetStockOutItemsPaged(SiteSettings.StockAlertLevel, orderBy, startIndex, pageSize);
            for (int i = 0; i < stockAlerts.Count; i++)
            {
                try
                {
                    TblProductStock stock = stockAlerts[i];
                    Product product = Product.Load(stock.ProductId);
                    StockAlert alert = new StockAlert(stock);
                    alerts.Add(alert);
                }
                catch (Exception ex) { }
            }

            return alerts;
        }

        public static int GetStockOutListCount()
        {
            int count = 0;
            count = TblProductStockExtended.GetStockOutItemsCount(SiteSettings.StockAlertLevel);
            return count;
        }

        public static void SendStockAlertEmail(StockAlert alert)
        {
            Email.SendStockAlertEmailToAdmin(alert);
        }
        public static void SendStockAlertEmail(ReadWriteCustomCollection<StockAlert> alerts)
        {
            foreach (StockAlert alert in alerts)
            {
                Email.SendStockAlertEmailToAdmin(alert);
            }

        }
    }
}
